package com.cloud.hotline.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.hotline.api.bean.HotlineCleanForm;
import com.cloud.hotline.api.entity.ToHotlineRecord;
import com.cloud.hotline.api.mapper.ToHotlineRecordMapper;
import com.cloud.hotline.api.service.HotlineCleanService;
import com.cloud.hotline.common.utils.DateHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class HotlineCleanServiceImpl implements HotlineCleanService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private ToHotlineRecordMapper toHotlineRecordMapper;

	@Override
	public PageInfo<ToHotlineRecord> list(final HotlineCleanForm form) {
		return PageHelper.startPage(form.getPageNum(), form.getPageSize()).doSelectPageInfo(() -> {
			Example example = new Example(ToHotlineRecord.class);
			Example.Criteria criteria = example.createCriteria();
			int dateType = form.getType();
			if (dateType == 0) {
				if (form.getRevisitType() == 1) {
					dateType = 1;
				}
				if (form.getRevisitType() == 2) {
					dateType = 2;
				}
			} else {
				dateType = dateType + 2;
			}
			criteria.andEqualTo("dataTypeId", dateType);
			criteria.andGreaterThanOrEqualTo("stateDate", form.getStart());
			criteria.andLessThanOrEqualTo("stateDate", form.getEnd());
			if (dateType == 1) {
				// 市级回访
				criteria.andIsNotNull("cityRevisitDate");
				criteria.andIsNotNull("cityPleasedFlagName");
				criteria.andIsNotNull("cityFinishFlagName");
			} else if (dateType == 2) {
				// 区级回访
				criteria.andIsNotNull("revisitDate");
				criteria.andIsNotNull("pleasedFlagName");
				criteria.andIsNotNull("finishFlagName");
			} else if (dateType == 3) {
				// 街道承办
			} else if (dateType == 4) {
				// 部门承办
			}
			toHotlineRecordMapper.selectByExample(example);
		});
	}

	@Override
	public int submit(HotlineCleanForm form) {
		int i = 0;
		int dataType = form.getType();
		if (dataType == 0) {
			if (form.getRevisitType() == 1) {
				dataType = 1;
			}
			if (form.getRevisitType() == 2) {
				dataType = 2;
			}
		} else {
			dataType = dataType + 2;
		}
		if (null == form.getStart()) {
			form.setStart(DateHelper.strToDateTime("1990-01-01 00:00:00"));
		}
		if (null == form.getEnd()) {
			form.setEnd(DateHelper.strToDateTime("2099-12-31 00:00:00"));
		}
		// 需求不明确，先分开写
		if (dataType == 1) {
			// 市级回访
			i = jdbcTemplate
					.update("update to_hotline_record set data_type_id = 0 - data_type_id, city_revisit_date = null, last_modify_time = now(), "
							+ "city_pleased_flag_name = null, city_finish_flag_name = null where data_type_id = ? "
							+ "and state_date >= ? and state_date <= ?", dataType, form.getStart(), form.getEnd());
		} else if (dataType == 2) {
			// 区级回访
			i = jdbcTemplate
					.update("update to_hotline_record set data_type_id = 0 - data_type_id, revisit_date = null, pleased_flag_name = null,"
							+ "finish_flag_name = null, last_modify_time = now() where data_type_id = ? "
							+ "and state_date >= ? and state_date <= ?", dataType, form.getStart(), form.getEnd());
		} else if (dataType == 3) {
			// 街道承办
			i = jdbcTemplate
					.update("update to_hotline_record set data_type_id = 0 - data_type_id, last_modify_time = now() where data_type_id = ? "
							+ "and state_date >= ? and state_date <= ?", dataType, form.getStart(), form.getEnd());
		} else if (dataType == 4) {
			// 部门承办
			i = jdbcTemplate
					.update("update to_hotline_record set data_type_id = 0 - data_type_id, last_modify_time = now() where data_type_id = ? "
							+ "and state_date >= ? and state_date <= ?", dataType, form.getStart(), form.getEnd());
		}
		return i;
	}

}
